package com.hskn.hss.module.workruleitem.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.workruleitem.entity.WorkRuleItem;
import com.hskn.hss.module.workruleitem.entity.WorkRuleItemFull;
import com.hskn.hss.module.workruleitem.mapper.WorkRuleItemMapper;
import com.hskn.hss.module.workruleitem.service.IWorkRuleItemService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-12
 */
@Service
public class WorkRuleItemServiceImpl extends ServiceImpl<WorkRuleItemMapper, WorkRuleItem> implements IWorkRuleItemService {

    @Resource
    private WorkRuleItemMapper workRuleItemMapper;

    @Override
    public List<WorkRuleItemFull> init(JSONObject params) throws HssException {
        if (StringUtils.isEmpty(params) || !params.containsKey("type") || StringUtils.isEmpty(params.getInteger("type"))) {
            throw new HssException("参数错误");
        }
        String r = "";
        // 道路保洁2，机械清扫3，垃圾清运4，垃圾转运5，机动作业6
        Integer type = params.getInteger("type");
        switch (type) {
            case 2:
                r = "dlbj_default";
                break;
            case 3:
                r = "jxqs_default";
                break;
            case 4:
                r = "ljqy_default";
                break;
            case 5:
                r = "ljzy_default";
                break;
            default:
                break;
        }
        QueryWrapper<WorkRuleItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WorkRuleItem::getRuleId, r);
        List<WorkRuleItem> workRuleItems = workRuleItemMapper.selectList(queryWrapper);
        List<WorkRuleItemFull> workRuleItemFulls = new ArrayList<>();
        for (WorkRuleItem item : workRuleItems) {
            WorkRuleItemFull workRuleItemFull = new WorkRuleItemFull();
            workRuleItemFull.setContent(StringUtils.isEmpty(item.getContent()) ? new JSONObject() : JSONObject.parseObject(item.getContent()));
            workRuleItemFull.setCustomName(item.getCustomName());
            workRuleItemFull.setIsEnable(item.getIsEnable());
            workRuleItemFull.setRemark(item.getRemark());
            workRuleItemFull.setSysName(item.getSysName());
            workRuleItemFulls.add(workRuleItemFull);
        }
        return workRuleItemFulls;
    }

    @Override
    public void mUpdate(JSONObject params) throws HssException {
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("workRuleItemId"))) {
            throw new HssException("参数错误");
        }
        WorkRuleItem ruleItem = JSON.parseObject(params.toJSONString(), WorkRuleItem.class);
        workRuleItemMapper.updateById(ruleItem);
    }
}
